package com.ybkj.daijia.aop;

import com.ybkj.daijia.api.member.ErrorConstant;
import com.ybkj.daijia.api.member.transport.ResultTransport;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class BossControllerAop {

    private static final Logger logger = LoggerFactory.getLogger(BossControllerAop.class);

    @Pointcut("execution(public * com.ybkj.daijia.api.boss.v1.BossController.createOrder(..))")
    public void pointCutBossCreateOrder() {

    }

    @Pointcut("execution(public * com.ybkj.daijia.api.boss.v1.BossController.todayOrder(..))")
    public void pointCutBossTodayOrder() {

    }


    @Around("pointCutBossCreateOrder()")
    public Object BossCreateOrder(ProceedingJoinPoint thisJoinPoint) throws Throwable {

        boolean flag = false;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
            .getRequestAttributes()).getRequest();

        String UserAgent = request.getHeader("User-Agent");
        if (StringUtils.isBlank(UserAgent)) {
            return ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
        }
        if (!UserAgent.contains("yimidaijia_boss") && !UserAgent.contains("okhttp")) {
            return ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
        }

        Object args[] = thisJoinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            if (null != args[i] && StringUtils.isNotBlank(String.valueOf(args[i]))) {
                if (String.valueOf(args[i]).toUpperCase().contains("UNION")) {
                    flag = true;
                    break;
                } else if (String.valueOf(args[i]).toUpperCase().contains("CONCAT")) {
                    flag = true;
                    break;
                } else if (String.valueOf(args[i]).toUpperCase().contains("SELECT")) {
                    flag = true;
                    break;
                } else if (String.valueOf(args[i]).toUpperCase().contains("INSERT")) {
                    flag = true;
                    break;
                } else if (String.valueOf(args[i]).toUpperCase().contains("UPDATE")) {
                    flag = true;
                    break;
                } else if (String.valueOf(args[i]).toUpperCase().contains("DELETE")) {
                    flag = true;
                    break;
                }
            }
        }

        if (flag) {
            String ip = getIpAddress(request);
            logger.error("BossControllerAop-ip:" + ip);

            return ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
        }

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ResultTransport result = (ResultTransport) thisJoinPoint.proceed(args);
        stopWatch.stop();
        return result;

    }

    @Around("pointCutBossTodayOrder()")
    public Object BossTodayOrder(ProceedingJoinPoint thisJoinPoint) throws Throwable {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
            .getRequestAttributes()).getRequest();

        String UserAgent = request.getHeader("User-Agent");
        if (StringUtils.isBlank(UserAgent)) {
            return ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
        }
        if (!UserAgent.contains("yimidaijia_boss") && !UserAgent.contains("okhttp")) {
            return ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
        }

        Object args[] = thisJoinPoint.getArgs();

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ResultTransport result = (ResultTransport) thisJoinPoint.proceed(args);
        stopWatch.stop();
        return result;

    }

    public String getIpAddress(HttpServletRequest request) {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址  

        String ip = request.getHeader("X-Forwarded-For");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

}